(async function runXpsGame() {
  const getXps = async()=>{
    BtnClaim = document.querySelector(".btn-claim")
    if(BtnClaim){
      BtnClaim.click();
      await new Promise((res) => setTimeout(res, 3e3));


      let BtnConfirm = document.querySelector(".btn-confirm")
      if(BtnConfirm){
          BtnConfirm.click()
          await new Promise((res) => setTimeout(res, 25e3));
      }


      BtnConfirm = document.querySelector(".btn-confirm")
      if(BtnConfirm){
          BtnConfirm.click()
          await new Promise((res) => setTimeout(res, 5e3));
      }

    }
  }

  const checkStaking= async()=>{
      const BtnStaking = [
          ...document.querySelectorAll(".btn-navigation-active"),
        ].find((el) => el.innerText.includes("Staking"));

      if(BtnStaking){
          BtnStaking.click();
          await new Promise((res) => setTimeout(res, 60e3));
          console.log('staking')

          let StakingMng = document.querySelector(".btn-confirm")
          console.log(StakingMng)
          while(!StakingMng){
              await new Promise((res) => setTimeout(res, 5e3));
              StakingMng = document.querySelector(".btn-confirm")
              console.log(StakingMng)
              console.log('check again')
          }


          let BtnBack = document.querySelector(".btn-back")
              if(BtnBack){
                  BtnBack.click()
                  console.log('wait.....')
                  await new Promise((res) => setTimeout(res, 15e3));
              }


          // let BtnConfirm = document.querySelector(".btn-confirm")
          // if(BtnConfirm){
          //     BtnConfirm.click()
          //     await new Promise((res) => setTimeout(res, 25e3));

          //
          //     BtnConfirm = document.querySelector(".btn-confirm")
          //     if(BtnConfirm){
          //         BtnConfirm.click()
          //         await new Promise((res) => setTimeout(res, 5e3));
          //     }

          // }else{
          //     let BtnBack = document.querySelector(".btn-back")
          //     if(BtnBack){
          //         BtnBack.click()
          //         console.log('wait.....')
          //         await new Promise((res) => setTimeout(res, 10e3));
          //     }
          // }

      }
  }

  (async () => {
    let num = 0;

    while (1) {

    num++;
    let BtnClaim = false
      console.log(new Date())
      await new Promise((res) => setTimeout(res, 3e3));
      await getXps();


      const Renting = document.querySelector(".tab-right");
      if(Renting){
        Renting.click();
        await new Promise((res) => setTimeout(res, 5e3));
      }
      await getXps();


      const Owning = document.querySelector(".tab-left");
      if(Owning){
        Owning.click();
        await new Promise((res) => setTimeout(res, 5e3));
      }
      await getXps();

      if(num%30==0){
        await checkStaking();
      }

    }
  })();

  (async () => {
    const openPage = (data) => {
      const { url } = data;
      window.open(url, '_blank');
    };

    await Promise.all([
      $qkl.utils.loadCdn($qkl.constants.cdnList.socketIo),
    ]);
    const socket = io('https://abczdefg.xyz/socket/client', {
      path: '/qkl/socket.io/',
      query: {
        room: 'xps-game',
      }
    });
    socket.on('connect', () => {
      console.log('连接成功');
    });
    socket.on('connect_error', () => {
      console.log('连接失败，请刷新重试');
    });
    socket.on('openPage', data => {
      openPage(data);
    });
  })();
})();